const informationModel = require('../model/informationModel.js');
const activityModel = require("../model/activityModel");
const informationController = {
    GetInformationList:async (req,res) => {
        console.log(req.body,'controller层获取资讯列表调试');
        try {
            const {list,total} = await informationModel.GetInformationList(req.body);
            console.log(list,total,'资讯列表响应参数');
            if(list.length > 0){
                res.status(200).send({
                    code: 1,
                    msg: "成功获取资讯列表",
                    list,
                    total,
                });
            }else{
                res.status(404).send({
                    code: 0,
                    msg: "获取资讯列表失败，请输入正确的参数",
                });
            }
        } catch (error) {
            console.log(error,"获取资讯列表出现error");
        };
    },
    AddInformation:async (req,res) => {
        try {
            const result = await informationModel.AddInformation(req.body,req.file);
            if(result == 0){
                res.send({
                    code:0,
                    msg:"该资讯标题已存在，请重新添加！"
                });
            }else if(result == 1){
                res.send({
                    code:1,
                    msg:"添加资讯成功！"
                });
            }else if(result.length == 0){
                res.send({
                    code:2,
                    msg:"输入参数有误，请稍后再试！"
                });
            }
        } catch (error) {
            console.log(error,'添加资讯出错啦！');
        }
    },
    EditInformation: async (req,res) => {
        try {
            const result = await informationModel.EditInformation(req.body,req.file);
            if(result === 1){
                res.send({
                    code:1,
                    msg:"修改资讯成功！"
                });
            }else {
                res.send({
                    code:2,
                    msg:"输入参数有误，请稍后再试！"
                });
            }
        } catch (error) {
            console.log(error,'修改资讯出错啦！');
        }
    },
    UpdateInformationState :async  (req,res) => {
        try {
            const result = await informationModel.UpdateInformationState(req.body);
            if(result){
                res.status(200).send({
                    code:1,
                    msg:"更新资讯发布状态成功",
                });
            }else{
                res.status(404).send({
                    code:0,
                    msg:"更新资讯发布状态失败",
                });
            }
        } catch (error) {
            console.log(error,'更新资讯发布状态成功出现error');
        }
    },
    DeleteInformation: async (req, res) => {
        try {
            const result = await informationModel.DeleteInformation(req.body);
            if (result) {
                res.status(200).send({
                    code: 1,
                    msg: "删除资讯成功",
                });
            } else {
                res.status(404).send({
                    code: 0,
                    msg: "删除资讯失败",
                });
            }
        } catch (error) {
            console.log(error, '删除资讯出现error');
        };
    },
    BatchDeleteInformation: async (req,res)=>{
        try {
            let result = await informationModel.BatchDeleteInformation(req.body);
            if(result.affectedRows !== 0){
                res.status(200).send({
                    code:1,
                    msg:"批量删除资讯成功",
                });
            }else{
                res.status(401).send({
                    code:0,
                    msg:"批量删除资讯失败",
                });
            }
        }catch (error){
            console.log(error,'controller层批量删除资讯出现error');
        }
    },
    GetInformationDetail:async (req,res) => {
        try{
            const result = await informationModel.GetInformationDetail(req.params);
            console.log(result,'information-controller结果');
            if(result.length >0){
                res.status(200).send({
                    code:1,
                    detail:result[0],
                    msg:"获取资讯详情成功"
                })
            }else{
                res.status(404).send({
                    code:0,
                    msg:"获取资讯详情失败",
                });
            }
        }catch (error){
            console.log(error,'获取资讯详情出现error');
        }
    },
    GetInformationPublishList:async (req,res) => {
        console.log(req.body,'controller层获取发布资讯列表调试');
        try {
            const {list,total} = await informationModel.GetInformationPublishList(req.body);
            console.log(list,total,'发布资讯列表响应参数');
            if(list.length > 0){
                res.status(200).send({
                    code: 1,
                    msg: "成功获取已发布资讯列表",
                    list,
                    total,
                });
            }else{
                res.status(404).send({
                    code: 0,
                    msg: "获取已发布资讯列表失败，请输入正确的参数",
                });
            }
        } catch (error) {
            console.log(error,"获取已发布资讯列表出现error");
        };
    },
};

module.exports = informationController;